<?php
/**
 * Created on 7 mai 2011
 *  * @author Samy Deghou(deghou@polytech.unice.fr ; deghou@embl.de)
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
	require ('./Global/GlobalVariables.php');
	require ('./Global/GlobalFunctions.php');


/*
 * session_start() will initialize the global variable $_SESSION (type: array)
 */
	session_start();

date_default_timezone_set("Europe/Paris");

/*
 * Each time the file receive a request, it will load :
 * 		(i)   The proper controller if it exists
 * 		(ii)  If not it will directly load the view if it exists
 * 		(iii) Or just the model if it is an action that does not require to load a view or any controller
 * 			 (which in theory should never be the case)
 */
	function __autoload($name) {
//		echo($name);
		if(strstr($name, CONTROLLER_SUFFIXE)) {//User_Controller
			require ("./Controller/".$name.".php");
		} else if(strstr($name, VIEW_SUFFIXE)){//Login_User_View
				$view = explode('_', $name);
				if($view[1]=="JavaScript"){
					require ("./JavaScript/".$view[2]."/".$view[3]."/".$view[4].
							"/Visualiser_JavaScript_".$view[3]."_".$view[4]."_".$view[5]."_".$view[6].".php");			
				}else{
					require ("./View/".$view[1]."/".$name.".php");//../views/User/Login.php	
				}
		} else {
			$option1=file_exists("./Model/".$name.".php");
			$option2=file_exists("./Model/".$name.".class.php");
			if($option1){
				require_once("./Model/".$name.".php");
			}elseif($option2){
				require_once("./Model/".$name.".class.php");//User	
			}else if($name[0]=="/"){
				$option3=file_exists($name.".php");
				$option4=file_exists($name.".class.php");
				if($option3){
					require_once($name.".php");
				}elseif($option4){
					require_once($name.".class.php");//User	
				}
				require_once($name.".php");
			}
		}
	}

	if(!empty($_GET['controller'])) {
		if(_existController($_GET['controller'])) {
			$controllerClass = new ReflectionClass($_GET['controller'].CONTROLLER_SUFFIXE);
		} else {
			echo("THe controller does not exists");
			_redirect(INDEX_PAGE);
		}
		if($controllerClass->isInstantiable()) {
			$controllerObj = $controllerClass->newInstance();
			
			if(!empty($_GET['action'])) {
					$action = $_GET['action'];
					if(count($_GET) > 2) {
						$params = array();
						foreach($_GET as $getKey => $getValue) {
							if($getKey != "controller" && $getKey != "action") {
								$params[$getKey] = $getValue;
							}
						}
					} else {
						$params = null;
					}
					$action = new ReflectionMethod($_GET['controller'].CONTROLLER_SUFFIXE, $action);
					$action->invoke($controllerObj, $params);
			} else {
					$action = new ReflectionMethod($_GET['controller'].CONTROLLER_SUFFIXE, 'index');
					$action->invoke($controllerObj, null);
			}
		} else {
			_redirect(INDEX_PAGE);//si c'est pas instanciable (ou redirection vers page d'erreur)
		}
	} else {
		_redirect(INDEX_PAGE);//si aucun controller n'est defini
	}
	
	session_write_close();
?>
